#include "dr16.h"

/* ------ ----------------- Internal Data ----------------------------------- */
volatile unsigned char sbus_rx_buffer[Uart4_DMARX_Length] = {0};

uint16_t Channel_0,Channel_1,Channel_2,Channel_3,Switch_left,Switch_right;
int Mouse_X,Mouse_Y,Mouse_Z,Mouse_Left,Mouse_Right,KeyBoard_value;
/* ----------------------- Function Implements ---------------------------- */
/******************************************************************************
* @fn RC_Init
*
* @brief configure stm32 uart4 port
* - USART Parameters
* - 100Kbps
* - 8-N-1
* - DMA Mode
*
* @return None.
*  UART4 RX PC11
* @note This code is fully tested on STM32F405RGT6 Platform, You can port it
* to the other platform.
*/
void dr16_init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;
    DMA_InitTypeDef   dma;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//pc11
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE);// 

	USART_DeInit(UART4);

    //UART4_RX	  PC.11
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
    GPIO_Init(GPIOC, &GPIO_InitStructure);
/********************************************************************/	
	  //UART4
	USART_InitStructure.USART_BaudRate = 100000;//
 	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//
//	USART_InitStructure.USART_WordLength = USART_WordLength_9b;//
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//
//	USART_InitStructure.USART_Parity = USART_Parity_Even;//
 	USART_InitStructure.USART_Parity = USART_Parity_No;//
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//
//	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_Mode = USART_Mode_Rx;	//
    USART_Init(UART4, &USART_InitStructure); //
//  USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//
    USART_Cmd(UART4, ENABLE);                    //
/*******************************UART4 RX DMA config*****************************************/	
    dma.DMA_PeripheralBaseAddr = (uint32_t)&(UART4->DR);
    dma.DMA_MemoryBaseAddr = (uint32_t)sbus_rx_buffer; //
    dma.DMA_DIR = DMA_DIR_PeripheralSRC;
    dma.DMA_BufferSize = Uart4_DMARX_Length;
    dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
    dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    dma.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
    dma.DMA_Mode = DMA_Mode_Circular;
    dma.DMA_Priority = DMA_Priority_VeryHigh;
    dma.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA2_Channel3,&dma);

	USART_DMACmd(UART4,USART_DMAReq_Rx,ENABLE);//
    DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);//DMA2_Channel3
    DMA_Cmd(DMA2_Channel3, ENABLE);//DMA2_Channel3
	
/***********************DMA2_Channel3***************************/
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
  
	DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);//DMA2_Channel3
}

/***********DMA2_Channel3************/
//void DMA2_Channel3_IRQHandler() 
//{
//    if(DMA_GetITStatus(DMA2_IT_TC3) == SET)
//    {
//        DMA_ClearFlag(DMA2_FLAG_TC3);
//        DMA_ClearITPendingBit(DMA2_FLAG_TC3);

// 		  Channel_0 = (sbus_rx_buffer[0]| (sbus_rx_buffer[1] << 8)) & 0x07ff; //!< Channel 0
//  	  Channel_1 = (uint16_t)((sbus_rx_buffer[1] >> 3) | (sbus_rx_buffer[2] << 5)) & 0x07ff; //!< Channel 1
//  	  Channel_2 = (uint16_t)((sbus_rx_buffer[2] >> 6) | (sbus_rx_buffer[3] << 2) |(sbus_rx_buffer[4] << 10)) & 0x07ff;//!< Channel 2
//  	  Channel_3 = (uint16_t)((sbus_rx_buffer[4] >> 1) | (sbus_rx_buffer[5] << 7)) & 0x07ff; //!< Channel 3
// 		
// 		  Switch_left = (uint16_t)((sbus_rx_buffer[5] >> 4)& 0x000C) >> 2; //!< Switch left
// 		  Switch_right = (uint16_t)((sbus_rx_buffer[5] >> 4)& 0x0003); //!< Switch right
// 		
// 		  Mouse_X = sbus_rx_buffer[6] | (sbus_rx_buffer[7] << 8); //!< Mouse X axis
// 		  Mouse_Y = sbus_rx_buffer[8] | (sbus_rx_buffer[9] << 8); //!< Mouse Y axis
// 		  Mouse_Z = sbus_rx_buffer[10] | (sbus_rx_buffer[11] << 8); //!< Mouse Z axis
// 		  Mouse_Left = sbus_rx_buffer[12]; //!< Mouse Left Is Press ?
// 		  Mouse_Right=sbus_rx_buffer[13]; //!< Mouse Right Is Press ?
// 		  KeyBoard_value = sbus_rx_buffer[14] | (sbus_rx_buffer[15] << 8); //!< KeyBoard value

//// 	  printf("Channel_0:%d\tChannel_1:%d\tChannel_2:%d\tChannel_3:%d\r\n",Channel_0,Channel_1,Channel_2,Channel_3);

//    }
//}

